﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using ITAP.Core.Modelling.Primitives;
using ITAP.Core.Modelling;

namespace ITAP.ContourBuilder.Components.Dimensions
{
    [Serializable]
    public class ContourElementDimension : Dimension
    {
        private double offsetFromPrimitive;

        public virtual double OffsetFromPrimitive
        {
            get { return offsetFromPrimitive; }
            set { offsetFromPrimitive = value; }
        }

        public virtual ContourElement Element
        {
            get
            {
                foreach (IContourComponent component in this.connectedComponents)
                    if (component is ContourElement)
                        return component as ContourElement;
                return null;
            }
        }

        public override string DimensionText
        {
            get
            {
                if(this.connectedComponents.Count != 1)
                    throw new Exception();


                    if(this.PrefixSymbol != String.Empty)
                        return this.PrefixSymbol + " = " + this.Element.Length.ToString("0.0");
                    else
                        return this.Element.Length.ToString("0.0");
            }
        }

        public override void MoveWithOffset(ModelVector offset)
        {
            this.offsetFromPrimitive += offset.Y;
        }

        public override void AcceptDimensionVisitor(IDimensionVisitor visitor)
        {
            visitor.Visit(this);
        }

        public override void Recalculate()
        {
            if (this.Element != null)
            {
                this.Primitive = this.Element.Primitive.Clone() as IPrimitive;
                this.Primitive = this.Primitive.GetOffsetPrimitive(offsetFromPrimitive);
            }
        }

        public override System.Drawing.RectangleF BoundingBox
        {
            get
            {
                return Element.BoundingBox;
            }
        }
    }
}
